package com.ruoyi.web.controller.auth;

import org.springframework.stereotype.Component;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * @author mr.huang [jie88888822@163.com]
 * @devTime 2022-06-15
 */
@Component
public class HHJGiteeOauth {

	private static final String GITEE_CLIENT_ID = "e89111e0eea3f24f88ba73099eed889e7624a375086a08ed946323a29f8460d4";
	private static final String GITEE_CLIENT_SECRET = "582941da3a74f1ded42c90806991e3ef7fa8dc1ea39fc787095e59249a4c7156";

	private static final String REDIRECT_URI = "http://localhost/login";
	/**
	 * 授权地址
	 * @return
	 */
	public static String authorize() {
		// state参数: 用于第三方应用防止CSRF攻击
		return "https://gitee.com/oauth/authorize?client_id=" + GITEE_CLIENT_ID +
				"&response_type=code&redirect_uri=" + URLEncoder.encode(REDIRECT_URI) +
				"&scope=user_info&state="+ UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**获取accessToken
	 * @param code 请求编码
	 * @param code
	 * @return
	 */
	public static String accessToken(String code) {
		return "https://gitee.com/oauth/token?grant_type=authorization_code&code=" + code + "&client_id=" + GITEE_CLIENT_ID
				+ "&redirect_uri=" + REDIRECT_URI + "&client_secret=" + GITEE_CLIENT_SECRET;
	}

	/**
	 * 获取用户信息
	 * @param accessToken
	 * @return
	 */
	public static String userInfo(String accessToken) {
		return "https://gitee.com/api/v5/user?access_token=" + accessToken;
	}
}
